package com.chalmers.foa;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Window;
import android.view.WindowManager;

import com.chalmers.foa.core.Game;
import com.chalmers.foa.gui.menus.Menu;
import com.chalmers.foa.gui.menus.SelectLevel;

public class Main extends Activity {
	boolean started=false;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// Forces landscape mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);							// Removes the window title bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,		// Hides the notification bar   
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		if(started)
			finish();
		else{
			final Editor prefEditor = pref.edit();
			final int id = R.xml.astart;
			final Intent i =  new Intent(Main.this, Game.class);
			if (pref.getBoolean("tutorial", true)) {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setCancelable(true);
				builder.setTitle("Play Tutorial?");
				builder.setInverseBackgroundForced(true);
				builder.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
								started=true;
								i.putExtra("level", R.xml.atutorial001);
								finish();
								startActivity(i);
							}
						});
				builder.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
								started=true;
								prefEditor.putBoolean(
										"tutorial", false);
								prefEditor.commit();
								i.putExtra("level", id);
								finish();
								startActivity(i);
							}
						});
				AlertDialog alert = builder.create();
				alert.show();
			}
			else{
				i.putExtra("level", id);
				finish();
				startActivity(i);
			}
		}
		// Intent menuActivity = new Intent(Main.this, Menu.class);
		// this.startActivity(menuActivity);
	}
	
	 @Override
	 public void onResume() {
	 super.onResume();
	 if(started)
		 finish();
	 }
}